import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';

class TransitionDemo extends StatefulWidget {
  @override
  _TransitionDemoState createState() => _TransitionDemoState();
}

class _TransitionDemoState extends State<TransitionDemo>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  int _count = 0;

  @override
  void initState() {
    _controller = AnimationController(
      duration: Duration(seconds: 1),
      vsync: this,
    );
    // _controller.repeat();
    _controller.addListener(() {
      if (_controller.status == AnimationStatus.forward) {
        _count++;
      } else if (_controller.status == AnimationStatus.completed) {
        print(_count);
        _controller.reset();
        _count = 0;
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void _changeState() {
    setState(() {
      _controller.forward();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('AnimatedDemo')),
      body: Center(
        child: SlideTransition(
          position: Tween(begin: Offset(0, -0.2), end: Offset(0.1, 0.2))
              .chain(CurveTween(curve: Curves.easeInOut))
              .animate(_controller),
          child: Container(
            width: 300,
            height: 300,
            color: Colors.blue,
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _changeState,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
